#include "StdAfx.h"
#include "UIControl.h"
#include "UIClientDC.h"
#include "UIDrawTool.h"
#include "UIMemClientDC.h"
#include "UIWindowDC.h"
#include "UIGridImgPlus.h"
#include "UIMemDC.h"
#include "UIGlobalStyle.h"
#include <Shlwapi.h>
#include <assert.h>
#include <WindowsX.h>
#pragma comment(lib,"Shlwapi.lib")
CUIControl::CUIControl( HWND hWnd ): CUIWindowBase( hWnd ), m_pImg( NULL ), m_brushFill( NULL )
{
    assert( IsChild() );
    m_bPressed = FALSE;
    m_bHot = FALSE;
    m_clrText = IsEnable() ? CUIGlobalStyle::GetTextColor() : ::GetSysColor( COLOR_GRAYTEXT );
    m_clrFill = IsEnable() ? CUIGlobalStyle::GetFillColor() : ::GetSysColor( COLOR_BTNFACE );
    m_clrBorder = IsEnable() ? CUIGlobalStyle::GetEdgeLightColor() : ::GetSysColor( COLOR_INACTIVEBORDER );
    m_brushFill =::CreateSolidBrush( m_clrFill );
// 	Redraw();
// 	ParentReDrawPart();

    SetTransparent( TRUE );
}


CUIControl::~CUIControl( void )
{
    if ( NULL != m_pImg )
    {
        delete m_pImg;
        m_pImg = NULL;
    }

    if ( NULL != m_brushFill )
    {
        ::DeleteObject( m_brushFill );
        m_brushFill = NULL;
    }
}

COLORREF CUIControl::GetTextColor()
{
    return m_clrText;
}

void CUIControl::SetTextColor( COLORREF clrText )
{
    m_clrText = clrText;
}

void CUIControl::SetFillColor( COLORREF clrFill )
{
    m_clrFill = clrFill;
}

COLORREF CUIControl::GetFillColor()
{
    return m_clrFill;
}

void CUIControl::SetBorderColor( COLORREF clrBorder )
{
    m_clrBorder = clrBorder;
}

COLORREF CUIControl::GetBorderColor()
{
    return m_clrBorder;
}

HBRUSH CUIControl::GetFillBrush()
{
    return m_brushFill;
}

void CUIControl::LoadImage( LPCTSTR lpszImg, IMGDIRECTION imgDir, UINT imgCount )
{
    if ( PathFileExists( lpszImg ) )
    {
        GetImage()->LoadImg( lpszImg, imgDir, imgCount );
        RECT rcMargin;
        ::SetRect( &rcMargin, 3, 3, 3, 3 );
        GetImage()->SetMargin( rcMargin );
    }
}


void CUIControl::GetButtonRect( RECT& rect )
{
    ::GetClientRect( GetHwnd(), &rect );
}

LRESULT CUIControl::OnLButtonDown( WPARAM wParam, LPARAM lParam )
{
    SetPresss( TRUE );
    Redraw();
    return 0;
}

LRESULT CUIControl::OnLButtonUp( WPARAM wParam, LPARAM lParam )
{
    SetPresss( FALSE );
    Redraw();
    return 0;
}

LRESULT CUIControl::OnMouseMove( WPARAM wParam, LPARAM lParam )
{
    POINT ptCursor;
    ptCursor.x = GET_X_LPARAM( lParam );
    ptCursor.y = GET_Y_LPARAM( lParam );

    RECT rcClient = {0};
    ::GetClientRect( GetHwnd(), &rcClient );

    if ( ::PtInRect( &rcClient, ptCursor ) )
    {
        TRACKMOUSEEVENT tme = {0};
        tme.cbSize = sizeof( TRACKMOUSEEVENT );
        tme.dwFlags = TME_LEAVE;
        tme.dwHoverTime = HOVER_DEFAULT;
        tme.hwndTrack = GetHwnd();
        TrackMouseEvent( &tme );

        if ( !IsHot() )
        {
            SetHot( TRUE );

            if ( IsEnable() )
            {
                m_clrBorder = CUIGlobalStyle::GetEdgeHighLightColor();
            }

            Redraw();

        }
    }

    return 0;
}

LRESULT CUIControl::OnMouseLeave( WPARAM wParam, LPARAM lParam )
{
    SetHot( FALSE );

    if ( IsEnable() )
    {
        m_clrBorder = CUIGlobalStyle::GetEdgeLightColor();
    }

    Redraw();

    return 0;
}

void CUIControl::ParentReDrawPart()
{
    CUIDrawTool::InvalidataParent( GetHwnd() );
}

void CUIControl::Redraw()
{
//	ParentReDrawPart();
    CUIDrawTool::InvalidataSelf( GetHwnd() );


// 	CUIClientDC dc(GetHwnd());
// 	CUIMemDC MemDC(dc.GetDC(),rcClient);
//
// 	CUIWindowDC ParentDC(::GetParent(GetHwnd()));
// 	RECT rcParent={0};
// 	::GetWindowRect(::GetParent(GetHwnd()),&rcParent);
// 	RECT rcControl={0};
// 	::GetWindowRect(GetHwnd(),&rcControl);
// 	POINT pt={0};
// 	pt.x=rcControl.left-rcParent.left;
// 	pt.y=rcControl.top-rcParent.top;
// 	::BitBlt(MemDC.GetDC(),0,0,rcControl.right-rcControl.left,rcControl.bottom-rcControl.top,ParentDC.GetDC(),pt.x,pt.y,SRCCOPY);
//
// 	OnDraw(MemDC.GetDC());

}

BOOL CUIControl::IsPress()const
{
    return m_bPressed;
}

BOOL CUIControl::IsHot()const
{
    return m_bHot;
}

BOOL CUIControl::IsFocused()
{
    return ::GetFocus() == GetHwnd();
}

BOOL CUIControl::IsEnable()
{
    return IsWindowEnabled( GetHwnd() );
}

UINT CUIControl::GetCtrlID()
{
    return ::GetDlgCtrlID( GetHwnd() );
}

void CUIControl::SetHot( BOOL bHot )
{
    m_bHot = bHot;
}

void CUIControl::SetPresss( BOOL bPress )
{
    m_bPressed = bPress;
}

void CUIControl::OnDraw( HDC hDC )
{

}

void CUIControl::OnPaint()
{
    PAINTSTRUCT ps;
    HDC hDC = BeginPaint( GetHwnd(), &ps );
    CUIClientDC Memdc( GetHwnd() );
// 	RECT rcClient={0};
// 	::GetClientRect(GetHwnd(),&rcClient);
// 	CUIMemDC dc(clientDC.GetDC(),rcClient);
//	OnDraw(dc.GetDC());
//	CUIMemDC Memdc(hDC,ps.rcPaint);
    OnDraw( Memdc.GetDC() );
    ::EndPaint( GetHwnd(), &ps );
}

void CUIControl::SetTransparent( BOOL bTransparent/* =TRUE */ )
{
    if ( TRUE == bTransparent )
    {
        DWORD dwExStyle = GetExStyle();
        ::SetWindowLong( GetHwnd(), GWL_EXSTYLE, dwExStyle | WS_EX_TRANSPARENT );
    }
}

CUIGridImgPlus* CUIControl::GetImage()
{
    if ( NULL == m_pImg )
    {
        m_pImg = new CUIGridImgPlus();
    }

    return m_pImg;
}

BOOL CUIControl::IsChecked()
{
    return BST_CHECKED == IsDlgButtonChecked( GetParent( GetHwnd() ), ::GetDlgCtrlID( GetHwnd() ) );
}

LRESULT CUIControl::ProcessUIMsg( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    switch ( uMsg )
    {
    case WM_PAINT:
    {
        OnPaint();
        return TRUE;
    }
    break;

    case WM_ERASEBKGND:
    {
        RECT rcClient = {0};
        ::GetClientRect( GetHwnd(), &rcClient );
        HDC hDC = ( HDC )wParam;
        CUIMemDC dc( hDC, rcClient );

        CUIWindowDC ParentDC( ::GetParent( GetHwnd() ) );
        RECT rcParent = {0};
        ::GetWindowRect( ::GetParent( GetHwnd() ), &rcParent );
        RECT rcControl = {0};
        ::GetWindowRect( GetHwnd(), &rcControl );
        POINT pt = {0};
        pt.x = rcControl.left - rcParent.left;
        pt.y = rcControl.top - rcParent.top;
        ::BitBlt( dc.GetDC(), 0, 0, rcControl.right - rcControl.left, rcControl.bottom - rcControl.top, ParentDC.GetDC(), pt.x, pt.y, SRCCOPY );

        OnDraw( dc.GetDC() );
        return TRUE;
    }
    break;

    case WM_LBUTTONDOWN:
    {
//			CUIWindowBase::ProcessUIMsg(uMsg,wParam,lParam);
        /*return*/ OnLButtonDown( wParam, lParam );
    }
    break;

    case WM_LBUTTONUP:
    {
//			CUIWindowBase::ProcessUIMsg(uMsg,wParam,lParam);
        /*return*/ OnLButtonUp( wParam, lParam );
    }
    break;

    case WM_MOUSEMOVE:
    {
//			CUIWindowBase::ProcessUIMsg(uMsg,wParam,lParam);
        /*return*/ OnMouseMove( wParam, lParam );
    }
    break;

    case WM_MOUSELEAVE:
    {
//			CUIWindowBase::ProcessUIMsg(uMsg,wParam,lParam);
        /*return*/ OnMouseLeave( wParam, lParam );
    }
    break;
// 	case WM_COMMAND:
// 		{
// 			if (!IsEnable())
// 			{
// 				return TRUE;
// 			}
// 		}
// 		break;
    }

    return CUIWindowBase::ProcessUIMsg( uMsg, wParam, lParam );
}
